package com.itheima.behaviour.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.behaviour.dto.FollowBehaviorDto;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class FollowBehaviorListener {
    @Resource
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = BusinessConstants.MqConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void consumer(ConsumerRecord<String,String> msg){
        int p=msg.partition();
        Long o=msg.offset();
        String value=msg.value();
        log.debug("用户关注行为收集：p={},o={},val={}",p,o,value);
        //转成java bean对象
        FollowBehaviorDto followBehaviorDto = JSON.parseObject(value, FollowBehaviorDto.class);
        apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        log.debug("操作成功！");
    }
}
